//
//  PXTableObject.m
//  MacStockApp
//
//  Created by 英傑 翁 on 12/2/10.
//  Copyright (c) 2012年 翁英傑. All rights reserved.
//

#import "PXTableObject.h"
#import "GBTableColumnHeader.h"
#import "GBTableColumnHeader_basicSqlite.h"
#import "GBTableColumnHeader_quote.h"
#import "GBTableColumnHeader_2Row3Row.h"
#import "GBTableColumnHeader_node.h" 
#import "GBTableColumnHeader_position.h"  
#import "GBTableColumnHeader_position_bottom_sumaverage.h"  
#import "GBTableColumnHeader_Indicator.h"  
#import "GBTableColumnHeader_signal.h"
#import "GBTableColumnHeader_ObjectOtherKey.h"  
#import "GBTableColumnHeader_key.h"
#import "GBall.h"
#import "GBCalculationMethod_selfIndicator.h"
@implementation PXTableObject

@synthesize data_array_object ;
@synthesize data_array_node;
@synthesize data_array_onlyfortableObject;
@synthesize data_dic_onlyfortableObject;
 

- (id)init {
	
    if(self=[super init])
	{
          
          data_array_object=[[NSMutableArray alloc] init];  
          data_array_node=[[NSMutableArray alloc] init];          
          data_array_onlyfortableObject =[[NSMutableArray alloc] init]; 
          data_dic_onlyfortableObject =[[NSMutableDictionary alloc] init];           
        
    }
	
	return self;
}

#pragma mark -
#pragma mark NSCoder

- (void)encodeWithCoder:(NSCoder *)aCoder{
    
    [aCoder encodeObject:data_array_object forKey:@"PXTableObject_data_array_object"]; 
    [aCoder encodeObject:data_array_node forKey:@"PXTableObject_data_array_node"];  
    [aCoder encodeObject:data_array_onlyfortableObject forKey:@"PXTableObject_data_array_only_for_table"]; 
    [aCoder encodeObject:data_dic_onlyfortableObject forKey:@"PXTableObject_data_dic_onlyfortableObject"];     
    
   
}
- (id)initWithCoder:(NSCoder *)aDecoder{
    self=[super init];
    if (self) {
        data_array_object=[[aDecoder decodeObjectForKey:@"PXTableObject_data_array_object"] retain]; 
        data_array_node=[[aDecoder decodeObjectForKey:@"PXTableObject_data_array_node"] retain]; 
        data_array_onlyfortableObject=[[aDecoder decodeObjectForKey:@"PXTableObject_data_array_only_for_table"] retain]; 
        
         data_dic_onlyfortableObject=[[aDecoder decodeObjectForKey:@"PXTableObject_data_dic_onlyfortableObject"] retain];         
        
    }
    return self;
}
#pragma mark -
#pragma mark 單一股票資料 (陣列內存放的是:Data_Object )
-(NSUInteger)index_data_arrary_object_from_code:(NSString *)code{
    NSString *search;
    search=code;                
    
    NSUInteger idx = [ data_array_object  indexOfObjectPassingTest:
                      ^ BOOL (Data_Object* obj, NSUInteger idx, BOOL *stop)
                      {
                          BOOL result = ([[[obj table_data] objectForKey:DB_object_sqlite_basic_code] isEqualToString:search ]);
                          *stop = result;
                          return result;                                      
                          
                      }];                
    return idx;
    
    
    
    
}

-(Data_Object *)object_data_arrary_object_from_code:(NSString *)code{
    NSString *search;
    search=code;                
    
    NSUInteger idx = [ data_array_object  indexOfObjectPassingTest:
                      ^ BOOL (Data_Object* obj, NSUInteger idx, BOOL *stop)
                      {
                          BOOL result = ([[[obj table_data] objectForKey:DB_object_sqlite_basic_code] isEqualToString:search ]);
                          *stop = result;
                          return result;                                      
                          
                      }];                
    
    
    if (idx ==NSNotFound) {
        return nil;      
    }
    else{
        Data_Object *obj=[ data_array_object  objectAtIndex:idx];      
        return obj;
        
    }
    
    
  
    
}

#pragma mark -
#pragma mark data_array_object排序

-(void)sort_data_array_object_table_column_object_array:(RLTableColumnHeader *)table_column_object_array  identfiler_table_column:(NSString *)set_identfiler_table_column  sort_direction:(NSString *)direction isSort_Node_para:(BOOL) isSort_Node_para{
 
     NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];	      
    
    //1.有兩種排序：依照手動排序或是節點順序改變後的排序
    if (isSort_Node_para==NO) {
        //表示手動排序，所以記錄此次排序資料 
                         
        [prefs setObject:direction   forKey: @"sort_direction" ]; 
        [prefs setObject:set_identfiler_table_column   forKey: @"sort_dentfiler_table_column" ];        
        [prefs synchronize];  
    }
    else {
        //表示此處排序是節點的順序重組過後，依照上次的排序設定來排序
        direction=[prefs objectForKey:@"sort_direction"];
        set_identfiler_table_column=[prefs objectForKey:@"sort_dentfiler_table_column" ];
   
    }
    
    //2.進行排序
    if (![set_identfiler_table_column isEqualToString:@""]) {
        NSString *sort_key;
        NSString *identfiler_table_column_location;  //排列資料放在的位址 不同位置要有不同方法存取資料  
        
        //取出欄位資料
        NSMutableDictionary *column_data;
        column_data=[table_column_object_array object_table_column_array_from_column_identfiler:set_identfiler_table_column];    
        
        NSString *identfiler_table_column;
        
        //取出要以哪一個row key 當作排序準則
        sort_key=[column_data objectForKey:Table_Column_sort_Key];
        
        //針對哪一列key 之後 取出名稱
        identfiler_table_column=[column_data objectForKey:sort_key];    
        
        //找出此key 下 資料放在哪邊的位置上?
        NSMutableDictionary *column_data_key;
        column_data_key=[table_column_object_array object_table_column_array_from_column_identfiler:identfiler_table_column];
        
        identfiler_table_column_location= [column_data_key objectForKey:Table_Column_data_loactaionPath]; 
        
        //排序實作方法
        NSArray *sortedArray =nil;    
        
        if ([direction isEqualToString:@"NSAscendingSortIndicator"]) {
            
                      
            sortedArray= [ data_array_object  sortedArrayUsingComparator:^(id firstObject, id secondObject) {
                
                if ([identfiler_table_column_location isEqualToString:loactaionPath_tableobject_data_array_node]) {
                    //針對節點特別處理 主要是非 table_data的資料都要另外排序
                    NSString *code1 = [[(Data_Object *)firstObject table_data] objectForKey:DB_object_sqlite_basic_code];
                    
                    NSString *code2 = [[(Data_Object *)secondObject table_data] objectForKey:DB_object_sqlite_basic_code]; 
                    
                    NSMutableDictionary *aad1;
                    aad1= [self object_data_arrary_node_from_code:code1];                                
                    
                    NSMutableDictionary *aad2;
                    aad2= [self object_data_arrary_node_from_code:code2];                                    
                    
                    
                    
                    //針對不同物件形態比較資料
                    if ([[aad1 objectForKey:identfiler_table_column] isKindOfClass:[NSString class]  ]) {
                        
                        
                        NSString *lastName1 = [aad1 objectForKey:identfiler_table_column] ;                               
                        NSString *lastName2 = [aad2 objectForKey:identfiler_table_column] ; 
                        
                        return [lastName1 caseInsensitiveCompare:lastName2];                            
                    } 
                    else if ([[aad1 objectForKey:identfiler_table_column] isKindOfClass:[NSNumber class]  ]) {
                        
                        
                        
                        NSNumber *lastName1 = [aad1 objectForKey:identfiler_table_column] ;                               
                        NSNumber *lastName2 = [aad2 objectForKey:identfiler_table_column] ; 
                        
                        return [lastName1 compare: lastName2];                            
                        
                    }  
                    else if ([[aad1 objectForKey:identfiler_table_column] isKindOfClass:[NSDate class]  ]) {
                        
                        
                        
                        NSDate *lastName1 = [aad1 objectForKey:identfiler_table_column] ;                               
                        NSDate *lastName2 = [aad2 objectForKey:identfiler_table_column] ;                                
                        
                        //注意這裡的順序
                        return [lastName1 compare: lastName2];                                    
                        
                        
                        
                        
                    }                           
                    else {
                        
                        NSString *lastName1 = [aad1 objectForKey:identfiler_table_column] ;                               
                        NSString *lastName2 = [aad2 objectForKey:identfiler_table_column] ;                                
                        
                        //注意這裡的順序
                        return [lastName1 caseInsensitiveCompare:lastName2];                             
                        
                    }                                  
                    
                    
                    
                    
                    
                    
                }
                else if ([identfiler_table_column_location isEqualToString:loactaionPath_tableobject_data_array_onlyfortableObject]) {
                    //針對節點特別處理 主要是onlyfortableObject
                    NSString *code1 = [[(Data_Object *)firstObject table_data] objectForKey:DB_object_sqlite_basic_code];
                    
                    NSMutableDictionary *aad1;
                    aad1= [self object_data_arrary_only_for_table_from_code:code1];                                
                    
                    
                    
                    NSString *code2 = [[(Data_Object *)secondObject table_data] objectForKey:DB_object_sqlite_basic_code];
                    
                    NSMutableDictionary *aad2;
                    aad2= [self object_data_arrary_only_for_table_from_code:code2];                                
                    
                    if ([aad1 objectForKey:identfiler_table_column] && [aad2 objectForKey:identfiler_table_column] ) {   
                        
                        //針對不同物件形態比較資料
                        if ([[aad1 objectForKey:identfiler_table_column] isKindOfClass:[NSString class]  ]) {
                            
                            
                            NSString *lastName1 = [aad1 objectForKey:identfiler_table_column] ;                                 
                            NSString *lastName2 = [aad2 objectForKey:identfiler_table_column] ;    
                            
                            return [lastName1 caseInsensitiveCompare:lastName2];                            
                        } 
                        else if ([[aad1 objectForKey:identfiler_table_column] isKindOfClass:[NSNumber class]  ]) {
                            
                            
                            
                            NSNumber *lastName1 = [aad1 objectForKey:identfiler_table_column] ;                                 
                            NSNumber *lastName2 = [aad2 objectForKey:identfiler_table_column] ;    
                            
                            return [lastName1 compare: lastName2];                            
                            
                        }  
                        else if ([[aad1 objectForKey:identfiler_table_column] isKindOfClass:[NSDate class]  ]) {
                            
                            
                            
                            NSDate *lastName1 = [aad1 objectForKey:identfiler_table_column] ;                                 
                            NSDate *lastName2 = [aad2 objectForKey:identfiler_table_column] ;
                            
                            return [lastName1 compare: lastName2];                                
                            
                            
                            
                            
                        }                           
                        else {
                            
                            NSString *lastName1 = [aad1 objectForKey:identfiler_table_column] ;                                 
                            NSString *lastName2 = [aad2 objectForKey:identfiler_table_column] ;    
                            
                            return [lastName1 caseInsensitiveCompare:lastName2];                            
                            
                        }                                            
                        
                    }
                    else {
                        return (NSComparisonResult)NSOrderedAscending;
                    }                                          
                    
                    
                    
                    
                }                            
                else if ([identfiler_table_column_location isEqualToString:loactaionPath_tableobject_data_array_object_table_data]) {
                    
                    //先檢查比較的物件是不是存在
                    if ([[(Data_Object *)firstObject table_data] objectForKey:identfiler_table_column] && [[(Data_Object *)secondObject table_data] objectForKey:identfiler_table_column] ) {
                        
                        //針對不同物件形態比較資料
                        if ([[[(Data_Object *)firstObject table_data] objectForKey:identfiler_table_column] isKindOfClass:[NSString class]  ]) {
                            
                            
                            NSString *lastName1 = [[(Data_Object *)firstObject table_data] objectForKey:identfiler_table_column];
                            NSString *lastName2 = [[(Data_Object *)secondObject table_data] objectForKey:identfiler_table_column];
                            
                            return [lastName1 caseInsensitiveCompare:lastName2];                            
                        } 
                        else if ([[[(Data_Object *)firstObject table_data] objectForKey:identfiler_table_column] isKindOfClass:[NSNumber class]  ]) {
                            
                            
                            
                            NSNumber *lastName1 = [[(Data_Object *)firstObject table_data] objectForKey:identfiler_table_column];
                            NSNumber *lastName2 = [[(Data_Object *)secondObject table_data] objectForKey:identfiler_table_column];
                            
                            return [lastName1 compare: lastName2];                            
                            
                        }  
                        else if ([[[(Data_Object *)firstObject table_data] objectForKey:identfiler_table_column] isKindOfClass:[NSDate class]  ]) {
                            
                            
                            
                            NSDate *lastName1 = [[(Data_Object *)firstObject table_data] objectForKey:identfiler_table_column];
                            NSDate *lastName2 = [[(Data_Object *)secondObject table_data] objectForKey:identfiler_table_column];
                            
                            return [lastName1 compare: lastName2];                                
                            
                            
                            
                            
                        }                           
                        else {
                            
                            NSString *lastName1 = [[(Data_Object *)firstObject table_data] objectForKey:identfiler_table_column];
                            NSString *lastName2 = [[(Data_Object *)secondObject table_data] objectForKey:identfiler_table_column];
                            
                            return [lastName1 caseInsensitiveCompare:lastName2];                            
                            
                        }
                        
                    }                        
                    else {
                        
                        return (NSComparisonResult)NSOrderedAscending;
                    }                                      
                    
                    
                }
                //功能步驟：個股時間序列上新增資料集合步驟 功能步驟: 3.排序 u               
                else if ([identfiler_table_column_location isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_signal]) {
                    
                    //功能步驟：個股時間序列上新增資料集合步驟 功能步驟: 3.排序 u                       
                    NSString *his_dic_key=HIS_dic_signal;
                    
                    //先檢查比較的物件是不是存在
                    if ([(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key  ]   &&  [(Data_Object *)secondObject  get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ] ) {
                        
                        //針對不同物件形態比較資料
                        if ([[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key  ] isKindOfClass:[NSString class]  ]) {
                            
                            
                            NSString *lastName1 =[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSString *lastName2 =[(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            
                            return [lastName1 caseInsensitiveCompare:lastName2];                            
                        } 
                        else if ([[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ]isKindOfClass:[NSNumber class]  ]) {
                            
                            
                            
                            NSNumber *lastName1 = [(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key  ];
                            NSNumber *lastName2 = [(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column  HIS_dic_key:his_dic_key  ];
                            
                            return [lastName1 compare: lastName2];                            
                            
                        }  
                        else if ([[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column  HIS_dic_key:his_dic_key ] isKindOfClass:[NSDate class]  ]) {
                            
                            
                            
                            NSDate *lastName1 = [(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSDate *lastName2 = [(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            
                            return [lastName1 compare: lastName2];                                
                            
                            
                            
                            
                        }                           
                        else {
                            
                            NSString *lastName1 = [(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSString *lastName2 = [(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            
                            return [lastName1 caseInsensitiveCompare:lastName2];                            
                            
                        }                            
                        
                    }                        
                    else {
                        
                        return (NSComparisonResult)NSOrderedAscending;
                    }                                      
                    
                    
                }
                else if ([identfiler_table_column_location isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_indicator]) {
                    
                    NSString *his_dic_key=HIS_dic_indicator;
                    
                    //先檢查比較的物件是不是存在
                    if ([(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key  ]   &&  [(Data_Object *)secondObject  get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ] ) {
                        
                        //針對不同物件形態比較資料
                        if ([[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key  ] isKindOfClass:[NSString class]  ]) {
                            
                            
                            NSString *lastName1 =[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSString *lastName2 =[(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            
                            return [lastName1 caseInsensitiveCompare:lastName2];
                        }
                        else if ([[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ]isKindOfClass:[NSNumber class]  ]) {
                            
                            
                            
                            NSNumber *lastName1 = [(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key  ];
                            NSNumber *lastName2 = [(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column  HIS_dic_key:his_dic_key  ];
                            
                            return [lastName1 compare: lastName2];
                            
                        }
                        else if ([[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column  HIS_dic_key:his_dic_key ] isKindOfClass:[NSDate class]  ]) {
                            
                            
                            
                            NSDate *lastName1 = [(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSDate *lastName2 = [(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            
                            return [lastName1 compare: lastName2];
                            
                            
                            
                            
                        }
                        else {
                            
                            NSString *lastName1 = [(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSString *lastName2 = [(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            
                            return [lastName1 caseInsensitiveCompare:lastName2];
                            
                        }
                        
                    }
                    else {
                        
                        return (NSComparisonResult)NSOrderedAscending;
                    }
                    
                    
                }
                else if ([identfiler_table_column_location isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_profit]) {
                    
                    NSString *his_dic_key=HIS_dic_profit;
                    
                    //先檢查比較的物件是不是存在
                    if ([(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key  ]   &&  [(Data_Object *)secondObject  get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ] ) {
                        
                        //針對不同物件形態比較資料
                        if ([[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key  ] isKindOfClass:[NSString class]  ]) {
                            
                            
                            NSString *lastName1 =[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSString *lastName2 =[(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            
                            return [lastName1 caseInsensitiveCompare:lastName2];
                        }
                        else if ([[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ]isKindOfClass:[NSNumber class]  ]) {
                            
                            
                            
                            NSNumber *lastName1 = [(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key  ];
                            NSNumber *lastName2 = [(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column  HIS_dic_key:his_dic_key  ];
                            
                            return [lastName1 compare: lastName2];
                            
                        }
                        else if ([[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column  HIS_dic_key:his_dic_key ] isKindOfClass:[NSDate class]  ]) {
                            
                            
                            
                            NSDate *lastName1 = [(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSDate *lastName2 = [(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            
                            return [lastName1 compare: lastName2];
                            
                            
                            
                            
                        }
                        else {
                            
                            NSString *lastName1 = [(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSString *lastName2 = [(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            
                            return [lastName1 caseInsensitiveCompare:lastName2];
                            
                        }
                        
                    }
                    else {
                        
                        return (NSComparisonResult)NSOrderedAscending;
                    }
                    
                    
                }
                else if ([identfiler_table_column_location isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_temp]) {
                    
                    NSString *his_dic_key=HIS_dic_temp;
                    
                    //先檢查比較的物件是不是存在
                    if ([(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key  ]   &&  [(Data_Object *)secondObject  get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ] ) {
                        
                        //針對不同物件形態比較資料
                        if ([[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key  ] isKindOfClass:[NSString class]  ]) {
                            
                            
                            NSString *lastName1 =[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSString *lastName2 =[(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            
                            return [lastName1 caseInsensitiveCompare:lastName2];
                        }
                        else if ([[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ]isKindOfClass:[NSNumber class]  ]) {
                            
                            
                            
                            NSNumber *lastName1 = [(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key  ];
                            NSNumber *lastName2 = [(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column  HIS_dic_key:his_dic_key  ];
                            
                            return [lastName1 compare: lastName2];
                            
                        }
                        else if ([[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column  HIS_dic_key:his_dic_key ] isKindOfClass:[NSDate class]  ]) {
                            
                            
                            
                            NSDate *lastName1 = [(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSDate *lastName2 = [(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            
                            return [lastName1 compare: lastName2];
                            
                            
                            
                            
                        }
                        else {
                            
                            NSString *lastName1 = [(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSString *lastName2 = [(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            
                            return [lastName1 caseInsensitiveCompare:lastName2];
                            
                        }
                        
                    }
                    else {
                        
                        return (NSComparisonResult)NSOrderedAscending;
                    }
                    
                    
                }
                else   {
                    return (NSComparisonResult)NSOrderedAscending;
                }                          
                
            }];
        }
        else {
            
                    
            sortedArray= [data_array_object sortedArrayUsingComparator:^(id firstObject, id secondObject) {
                
                
                if ([identfiler_table_column_location isEqualToString:loactaionPath_tableobject_data_array_node]) {
                    //針對節點特別處理 主要是非 table_data的資料都要另外排序
                    NSString *code1 = [[(Data_Object *)firstObject table_data] objectForKey:DB_object_sqlite_basic_code];
                    
                    NSString *code2 = [[(Data_Object *)secondObject table_data] objectForKey:DB_object_sqlite_basic_code]; 
                    
                    NSMutableDictionary *aad1;
                    aad1= [self object_data_arrary_node_from_code:code1];                                
                    
                    NSMutableDictionary *aad2;
                    aad2= [self object_data_arrary_node_from_code:code2];                                    
                    
                    
                    
                    //針對不同物件形態比較資料
                    if ([[aad1 objectForKey:identfiler_table_column] isKindOfClass:[NSString class]  ]) {
                        
                        
                        NSString *lastName1 = [aad1 objectForKey:identfiler_table_column] ;                               
                        NSString *lastName2 = [aad2 objectForKey:identfiler_table_column] ; 
                        
                        return [lastName2 caseInsensitiveCompare:lastName1];                            
                    } 
                    else if ([[aad1 objectForKey:identfiler_table_column] isKindOfClass:[NSNumber class]  ]) {
                        
                        
                        
                        NSNumber *lastName1 = [aad1 objectForKey:identfiler_table_column] ;                               
                        NSNumber *lastName2 = [aad2 objectForKey:identfiler_table_column] ; 
                        
                        return [lastName2 compare: lastName1];                            
                        
                    }  
                    else if ([[aad1 objectForKey:identfiler_table_column] isKindOfClass:[NSDate class]  ]) {
                        
                        
                        
                        NSDate *lastName1 = [aad1 objectForKey:identfiler_table_column] ;                               
                        NSDate *lastName2 = [aad2 objectForKey:identfiler_table_column] ;                                
                        
                        //注意這裡的順序
                        return [lastName2 compare: lastName1];                                    
                        
                        
                        
                        
                    }                           
                    else {
                        
                        NSString *lastName1 = [aad1 objectForKey:identfiler_table_column] ;                               
                        NSString *lastName2 = [aad2 objectForKey:identfiler_table_column] ;                                
                        
                        //注意這裡的順序
                        return [lastName2 caseInsensitiveCompare:lastName1];                             
                        
                    }                                  
                    
                    
                    
                    
                    
                    
                }
                
                else if ([identfiler_table_column_location isEqualToString:loactaionPath_tableobject_data_array_onlyfortableObject]) {
                    //針對節點特別處理 主要是onlyfortableObject
                    NSString *code1 = [[(Data_Object *)firstObject table_data] objectForKey:DB_object_sqlite_basic_code];
                    
                    NSMutableDictionary *aad1;
                    aad1= [self object_data_arrary_only_for_table_from_code:code1];                                
                    
                    
                    
                    NSString *code2 = [[(Data_Object *)secondObject table_data] objectForKey:DB_object_sqlite_basic_code];
                    
                    NSMutableDictionary *aad2;
                    aad2= [self object_data_arrary_only_for_table_from_code:code2];                                
                    
                    if ([aad1 objectForKey:identfiler_table_column] && [aad2 objectForKey:identfiler_table_column] ) {   
                        
                        //針對不同物件形態比較資料
                        if ([[aad1 objectForKey:identfiler_table_column] isKindOfClass:[NSString class]  ]) {
                            
                            
                            NSString *lastName1 = [aad1 objectForKey:identfiler_table_column] ;                                 
                            NSString *lastName2 = [aad2 objectForKey:identfiler_table_column] ;    
                            
                            return [lastName2 caseInsensitiveCompare:lastName1];                            
                        } 
                        else if ([[aad1 objectForKey:identfiler_table_column] isKindOfClass:[NSNumber class]  ]) {
                            
                            
                            
                            NSNumber *lastName1 = [aad1 objectForKey:identfiler_table_column] ;                                 
                            NSNumber *lastName2 = [aad2 objectForKey:identfiler_table_column] ;    
                            
                            return [lastName2 compare: lastName1];                            
                            
                        }  
                        else if ([[aad1 objectForKey:identfiler_table_column] isKindOfClass:[NSDate class]  ]) {
                            
                            
                            
                            NSDate *lastName1 = [aad1 objectForKey:identfiler_table_column] ;                                 
                            NSDate *lastName2 = [aad2 objectForKey:identfiler_table_column] ;
                            
                            return [lastName2 compare: lastName1];                                
                            
                            
                            
                            
                        }                           
                        else {
                            
                            NSString *lastName1 = [aad1 objectForKey:identfiler_table_column] ;                                 
                            NSString *lastName2 = [aad2 objectForKey:identfiler_table_column] ;    
                            
                            return [lastName2 caseInsensitiveCompare:lastName1];                            
                            
                        }                                            
                        
                    }
                    else {
                        return (NSComparisonResult)NSOrderedAscending;
                    }                                          
                    
                    
                    
                    
                }                            
                else if ([identfiler_table_column_location isEqualToString:loactaionPath_tableobject_data_array_object_table_data]) {
                    
                    //先檢查比較的物件是不是存在
                    if ([[(Data_Object *)firstObject table_data] objectForKey:identfiler_table_column] && [[(Data_Object *)secondObject table_data] objectForKey:identfiler_table_column] ) {
                        
                        //針對不同物件形態比較資料
                        if ([[[(Data_Object *)firstObject table_data] objectForKey:identfiler_table_column] isKindOfClass:[NSString class]  ]) {
                            
                            
                            NSString *lastName1 = [[(Data_Object *)firstObject table_data] objectForKey:identfiler_table_column];
                            NSString *lastName2 = [[(Data_Object *)secondObject table_data] objectForKey:identfiler_table_column];
                            
                            return [lastName2 caseInsensitiveCompare:lastName1];                            
                        } 
                        else if ([[[(Data_Object *)firstObject table_data] objectForKey:identfiler_table_column] isKindOfClass:[NSNumber class]  ]) {
                            
                            
                            
                            NSNumber *lastName1 = [[(Data_Object *)firstObject table_data] objectForKey:identfiler_table_column];
                            NSNumber *lastName2 = [[(Data_Object *)secondObject table_data] objectForKey:identfiler_table_column];
                            
                            return [lastName2 compare: lastName1];                            
                            
                        }  
                        else if ([[[(Data_Object *)firstObject table_data] objectForKey:identfiler_table_column] isKindOfClass:[NSDate class]  ]) {
                            
                            
                            
                            NSDate *lastName1 = [[(Data_Object *)firstObject table_data] objectForKey:identfiler_table_column];
                            NSDate *lastName2 = [[(Data_Object *)secondObject table_data] objectForKey:identfiler_table_column];
                            
                            return [lastName2 compare: lastName1];                                
                            
                            
                            
                            
                        }                           
                        else {
                            
                            NSString *lastName1 = [[(Data_Object *)firstObject table_data] objectForKey:identfiler_table_column];
                            NSString *lastName2 = [[(Data_Object *)secondObject table_data] objectForKey:identfiler_table_column];
                            
                            return [lastName2 caseInsensitiveCompare:lastName1];                            
                            
                        }                            
                        
                    }                        
                    else {
                        
                        return (NSComparisonResult)NSOrderedAscending;
                    }                                      
                    
                    
                }      
               
                  //功能步驟：個股時間序列上新增資料集合步驟 功能步驟: 3.排序 d                
                else if ([identfiler_table_column_location isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_signal]) {
                     //功能步驟：個股時間序列上新增資料集合步驟 功能步驟: 3.排序 d
                    NSString *his_dic_key=HIS_dic_signal;
                    
                    //先檢查比較的物件是不是存在
                    if ([(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key  ]   &&  [(Data_Object *)secondObject  get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key  ] ) {
                        
                        //針對不同物件形態比較資料
                        if ([[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ] isKindOfClass:[NSString class]  ]) {
                            
                            
                            NSString *lastName1 =[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSString *lastName2 =[(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key  ];
                            
                            return [lastName2 caseInsensitiveCompare:lastName1];                            
                        } 
                        else if ([[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ]isKindOfClass:[NSNumber class]  ]) {
                            
                            
                            
                            NSNumber *lastName1 = [(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSNumber *lastName2 = [(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            
                            return [lastName2 compare: lastName1];                            
                            
                        }  
                        else if ([[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ] isKindOfClass:[NSDate class]  ]) {
                            
                            
                            
                            NSDate *lastName1 = [(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSDate *lastName2 = [(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            
                            return [lastName2 compare: lastName1];                                
                            
                            
                            
                            
                        }                           
                        else {
                            
                            NSString *lastName1 = [(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSString *lastName2 = [(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            
                            return [lastName2 caseInsensitiveCompare:lastName1];                            
                            
                        }                            
                        
                    }                        
                    else {
                        
                        return (NSComparisonResult)NSOrderedAscending;
                    }                                      
                    
                    
                }                      
                else if ([identfiler_table_column_location isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_indicator]) {
                    
                    NSString *his_dic_key=HIS_dic_indicator;
                    
                    //先檢查比較的物件是不是存在
                    if ([(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key  ]   &&  [(Data_Object *)secondObject  get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key  ] ) {
                        
                        //針對不同物件形態比較資料
                        if ([[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ] isKindOfClass:[NSString class]  ]) {
                            
                            
                            NSString *lastName1 =[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSString *lastName2 =[(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key  ];
                            
                            return [lastName2 caseInsensitiveCompare:lastName1];
                        }
                        else if ([[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ]isKindOfClass:[NSNumber class]  ]) {
                            
                            
                            
                            NSNumber *lastName1 = [(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSNumber *lastName2 = [(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            
                            return [lastName2 compare: lastName1];
                            
                        }
                        else if ([[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ] isKindOfClass:[NSDate class]  ]) {
                            
                            
                            
                            NSDate *lastName1 = [(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSDate *lastName2 = [(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            
                            return [lastName2 compare: lastName1];
                            
                            
                            
                            
                        }
                        else {
                            
                            NSString *lastName1 = [(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSString *lastName2 = [(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            
                            return [lastName2 caseInsensitiveCompare:lastName1];
                            
                        }
                        
                    }
                    else {
                        
                        return (NSComparisonResult)NSOrderedAscending;
                    }
                    
                    
                }
                else if ([identfiler_table_column_location isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_profit]) {
                    
                    NSString *his_dic_key=HIS_dic_profit;
                    
                    //先檢查比較的物件是不是存在
                    if ([(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key  ]   &&  [(Data_Object *)secondObject  get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key  ] ) {
                        
                        //針對不同物件形態比較資料
                        if ([[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ] isKindOfClass:[NSString class]  ]) {
                            
                            
                            NSString *lastName1 =[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSString *lastName2 =[(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key  ];
                            
                            return [lastName2 caseInsensitiveCompare:lastName1];
                        }
                        else if ([[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ]isKindOfClass:[NSNumber class]  ]) {
                            
                            
                            
                            NSNumber *lastName1 = [(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSNumber *lastName2 = [(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            
                            return [lastName2 compare: lastName1];
                            
                        }
                        else if ([[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ] isKindOfClass:[NSDate class]  ]) {
                            
                            
                            
                            NSDate *lastName1 = [(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSDate *lastName2 = [(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            
                            return [lastName2 compare: lastName1];
                            
                            
                            
                            
                        }
                        else {
                            
                            NSString *lastName1 = [(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSString *lastName2 = [(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            
                            return [lastName2 caseInsensitiveCompare:lastName1];
                            
                        }
                        
                    }
                    else {
                        
                        return (NSComparisonResult)NSOrderedAscending;
                    }
                    
                    
                }
                else if ([identfiler_table_column_location isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_temp]) {
                    
                    NSString *his_dic_key=HIS_dic_temp;
                    
                    //先檢查比較的物件是不是存在
                    if ([(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key  ]   &&  [(Data_Object *)secondObject  get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key  ] ) {
                        
                        //針對不同物件形態比較資料
                        if ([[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ] isKindOfClass:[NSString class]  ]) {
                            
                            
                            NSString *lastName1 =[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSString *lastName2 =[(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key  ];
                            
                            return [lastName2 caseInsensitiveCompare:lastName1];
                        }
                        else if ([[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ]isKindOfClass:[NSNumber class]  ]) {
                            
                            
                            
                            NSNumber *lastName1 = [(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSNumber *lastName2 = [(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            
                            return [lastName2 compare: lastName1];
                            
                        }
                        else if ([[(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ] isKindOfClass:[NSDate class]  ]) {
                            
                            
                            
                            NSDate *lastName1 = [(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSDate *lastName2 = [(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            
                            return [lastName2 compare: lastName1];
                            
                            
                            
                            
                        }
                        else {
                            
                            NSString *lastName1 = [(Data_Object *)firstObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            NSString *lastName2 = [(Data_Object *)secondObject get_last_his_data_serial_forkey:identfiler_table_column HIS_dic_key:his_dic_key   ];
                            
                            return [lastName2 caseInsensitiveCompare:lastName1];
                            
                        }
                        
                    }
                    else {
                        
                        return (NSComparisonResult)NSOrderedAscending;
                    }
                    
                    
                }
                
                else   {
                    return (NSComparisonResult)NSOrderedAscending;
                }                                                      
                
                
                
                
                
                
            }];        
            
            
        }
        
        //設定新的排序陣列 
        [data_array_object release];
        data_array_object=[sortedArray mutableCopy] ;          
        
        
    }
    
    
        
   
    
}

#pragma mark -
#pragma mark 存放節點從下層資料往上彙整的總和資料
 
-(NSUInteger)index_data_arrary_node_from_code:(NSString *)code{
    NSString *search;
    search=code;                
    
    NSUInteger idx = [data_array_node  indexOfObjectPassingTest:
                      ^ BOOL (NSMutableDictionary* obj, NSUInteger idx, BOOL *stop)
                      {
                          BOOL result = ([ [obj   objectForKey:DB_object_sqlite_basic_code] isEqualToString:search ]);
                          *stop = result;
                          return result;                                      
                          
                      }];                
    
     
    return idx;
    
    
    
    
}

-(NSMutableDictionary *)object_data_arrary_node_from_code:(NSString *)code{
    NSString *search;
    search=code;                
    
    NSUInteger idx = [ data_array_node  indexOfObjectPassingTest:
                      ^ BOOL (NSMutableDictionary* obj, NSUInteger idx, BOOL *stop)
                      {
                          BOOL result = ([[ obj  objectForKey:DB_object_sqlite_basic_code] isEqualToString:search ]);
                          *stop = result;
                          return result;                                      
                          
                      }];                
    
    
    if (idx ==NSNotFound) {
        return nil;      
    }
    else{
        NSMutableDictionary *obj=[ data_array_node  objectAtIndex:idx];      
        return obj;
        
    }
    
    
    
    
}
 
#pragma mark -
#pragma mark 存放的都是僅在此表格使用的資料

-(NSUInteger)index_data_arrary_only_for_table_from_code:(NSString *)code{
    NSString *search;
    search=code;                
    
    NSUInteger idx = [data_array_onlyfortableObject  indexOfObjectPassingTest:
                      ^ BOOL (NSMutableDictionary* obj, NSUInteger idx, BOOL *stop)
                      {
                          BOOL result = ([ [obj   objectForKey:DB_object_sqlite_basic_code] isEqualToString:search ]);
                          *stop = result;
                          return result;                                      
                          
                      }];                
    
    
    return idx;
    
    
    
    
}

-(NSMutableDictionary *)object_data_arrary_only_for_table_from_code:(NSString *)code{
    NSString *search;
    search=code;                
    
    NSUInteger idx = [ data_array_onlyfortableObject  indexOfObjectPassingTest:
                      ^ BOOL (NSMutableDictionary* obj, NSUInteger idx, BOOL *stop)
                      {
                          BOOL result = ([[ obj  objectForKey:DB_object_sqlite_basic_code] isEqualToString:search ]);
                          *stop = result;
                          return result;                                      
                          
                      }];                
    
    
    if (idx ==NSNotFound) {
        return nil;      
    }
    else{
        NSMutableDictionary *obj=[ data_array_onlyfortableObject  objectAtIndex:idx];      
        return obj;
        
    }
    
    
    
    
}

#pragma mark -
#pragma mark 計算表格上的-部位-欄位
//表格內所有股票進行計算
-(void)calculate_table_byarray_all:(NSString *)column_item{
    for (Data_Object *db in  data_array_object ) {
        
        NSString *code=@"";                
        
        if ([[db table_data] objectForKey:DB_object_sqlite_basic_code]  ) {
            code=[[db table_data] objectForKey:DB_object_sqlite_basic_code];  
        } 
        //
         
        [self calculate_table_byarray:column_item code:code];
    
    }    
    
}

//單一股票計算，需要計算的欄位 最後資料放的位置:object_data_arrary_only_for_table
-(void)calculate_table_byarray:(NSString *)column_item code:(NSString *)code{
    
    //標記：部位-自行輸入資料(此數據會啟動其他欄位被計算) 6.實作其他被啓動的需要計算的可能欄位    
    
    //標記：部位-數據是依據其他欄位資料計算出來 4.實作計算功能   
    
    //NSString *table_or_history=@""; //判斷是需要使用表格資料或是歷史資料計算
    NSString *colnum_name;
    
    
    //table_or_history=[stamp_array objectAtIndex:1];
    colnum_name=column_item;
    //取出股票通用物件資料
    Data_Object *obj=[self object_data_arrary_object_from_code:code];    
    
    //計算累計損益
    if ([colnum_name isEqualToString:DB_onlyfortableObject_positon_ProfitLoss]) {
        //計算部位損益
        
        
        //取出表格上股票資訊
        NSMutableDictionary *dic=[self object_data_arrary_only_for_table_from_code:code];
        
        //取出表格價格    
        //NSLog(@"Data_Objec=%@",[obj table_data]);
        //NSLog(@"dic=%@",dic);            
        
        //收盤價
        double close;
        close=[[[obj table_data] objectForKey:DB_object_quote_all_close] floatValue];
        
        //部位資訊
        int count;
        //double enter_price;
        
        double prfoitloss;
        
        double cost;            
        
        count=[[dic objectForKey:DB_onlyfortableObject_positon_count] intValue];
        cost=[[dic objectForKey:DB_onlyfortableObject_positon_cost] doubleValue];            
        
        
        //計算出損益
        prfoitloss= close*count-cost ;
        
        //存入資料
        [dic removeObjectForKey:DB_onlyfortableObject_positon_ProfitLoss];
        [dic setObject:[NSNumber numberWithDouble:prfoitloss] forKey:DB_onlyfortableObject_positon_ProfitLoss];
        
        //NSLog(@"code=%@ close=%f ",code,close); 
        //NSLog(@"count=%i ",count); 
        //NSLog(@"enter_price=%f ",enter_price);             
        //NSLog(@"bs=%i ",bs);             
    }
    //計算累計損益報酬率
    else if ([colnum_name isEqualToString:DB_onlyfortableObject_positon_ProfitLoss_percent]) {
        //計算部位損益
        
         
        //取出表格上股票資訊
        NSMutableDictionary *dic=[self object_data_arrary_only_for_table_from_code:code];
        
        //取出表格價格    
        //NSLog(@"Data_Objec=%@",[obj table_data]);
        //NSLog(@"dic=%@",dic);            
        
        //收盤價
        double close;
        close=[[[obj table_data] objectForKey:DB_object_quote_all_close] floatValue];
        
        //部位資訊
        int count;
        //double enter_price;
        
        double prfoitloss;
        
        
        
        double profitloss_percent;// 損益報酬率
        
        double cost;
        
        cost=[[dic objectForKey:DB_onlyfortableObject_positon_cost] doubleValue];            
        count=[[dic objectForKey:DB_onlyfortableObject_positon_count] intValue];
        
        
        
        //計算出損益
        prfoitloss= close *count-cost ;
        if (cost==0) {
            profitloss_percent=0;
        }
        else {
            profitloss_percent=prfoitloss/ cost *100 ;  
            //profitloss_percent=  entermarketvalue ;                  
            
        }
        
        //存入資料
        [dic removeObjectForKey:DB_onlyfortableObject_positon_ProfitLoss_percent];
        [dic setObject:[NSNumber numberWithDouble:profitloss_percent] forKey:DB_onlyfortableObject_positon_ProfitLoss_percent];
        
        
    }        
    //計算當日損益
    else if ([colnum_name isEqualToString:DB_onlyfortableObject_positon_ProfitLoss_today]) {
     
        //計算部位損益
        
        
        //取出表格上股票資訊
        NSMutableDictionary *dic=[self object_data_arrary_only_for_table_from_code:code];
        
        //取出表格價格    
        //NSLog(@"Data_Objec=%@",[obj table_data]);
        //NSLog(@"dic=%@",dic);            
        
        //漲跌
        double up_down;
        up_down=[[[obj table_data] objectForKey:DB_object_quote_all_updown] doubleValue];
        
        //部位資訊
        int count;
        
        
        double prfoitloss;
        
        count=[[dic objectForKey:DB_onlyfortableObject_positon_count] intValue];
        
        
        //計算今日出損益
        prfoitloss=up_down*count ;
        
        //存入資料
        [dic removeObjectForKey:DB_onlyfortableObject_positon_ProfitLoss_today];
        [dic setObject:[NSNumber numberWithDouble:prfoitloss] forKey:DB_onlyfortableObject_positon_ProfitLoss_today];
        
        //NSLog(@"code=%@ close=%f ",code,close); 
        //NSLog(@"count=%i ",count); 
        //NSLog(@"enter_price=%f ",enter_price);             
        //NSLog(@"bs=%i ",bs);             
    }
    //計算市值
    else if ([colnum_name isEqualToString:DB_onlyfortableObject_positon_marketvalue]) {
        //計算市值
         
        //取出表格上股票資訊
        NSMutableDictionary *dic=[self object_data_arrary_only_for_table_from_code:code];
        
        //取出表格價格    
        //NSLog(@"Data_Objec=%@",[obj table_data]);
        //NSLog(@"dic=%@",dic);            
        
        //收盤價
        double close;
        close=[[[obj table_data] objectForKey:DB_object_quote_all_close] doubleValue];
        
        //部位資訊
        int count;
        
        
        double prfoitloss;
        
        count=[[dic objectForKey:DB_onlyfortableObject_positon_count] intValue];
        
        
        //計算今日出損益
        prfoitloss=close*count ;
        
        //存入資料
        [dic removeObjectForKey:DB_onlyfortableObject_positon_marketvalue];
        [dic setObject:[NSNumber numberWithDouble:prfoitloss] forKey:DB_onlyfortableObject_positon_marketvalue];
        
        //NSLog(@"code=%@ close=%f ",code,close); 
        //NSLog(@"count=%i ",count); 
        //NSLog(@"enter_price=%f ",enter_price);             
        //NSLog(@"bs=%i ",bs);             
    }  
    //手動計算成本
    else if ([colnum_name isEqualToString:DB_onlyfortableObject_positon_cost]) {
        //計算部位損益
        
        //取出股票通用物件資料
        //Data_Object *obj=[self object_data_arrary_object_from_code:code];
        //取出表格上股票資訊
        NSMutableDictionary *dic=[self object_data_arrary_only_for_table_from_code:code];
        
        //取出表格價格    
        //NSLog(@"Data_Objec=%@",[obj table_data]);
        //NSLog(@"dic=%@",dic);            
        
        //收盤價
        //double close;
        //close=[[[obj table_data] objectForKey:DB_object_quote_all_close] floatValue];
        
        //部位資訊
        int count;
        double enter_price;
        
        double cost;
        
        count=[[dic objectForKey:DB_onlyfortableObject_positon_count] intValue];
        enter_price=[[dic objectForKey:DB_onlyfortableObject_positon_EnterPrice] doubleValue];            
        
        //計算成本
        cost= enter_price *count ;
        
        //存入資料
        [dic removeObjectForKey:DB_onlyfortableObject_positon_cost];
        [dic setObject:[NSNumber numberWithDouble:cost] forKey:DB_onlyfortableObject_positon_cost];
        
        //NSLog(@"code=%@ close=%f ",code,close); 
        //NSLog(@"count=%i ",count); 
        //NSLog(@"enter_price=%f ",enter_price);             
        //NSLog(@"bs=%i ",bs);             
    }        
    //範例
    else if ([colnum_name isEqualToString:DB_onlyfortableObject_positon_valueChangeSample]) {
        //計算部位損益
        
        //取出股票通用物件資料
        //Data_Object *obj=[self object_data_arrary_object_from_code:code];
        //取出表格上股票資訊
        NSMutableDictionary *dic=[self object_data_arrary_only_for_table_from_code:code];
        
        //取出表格價格    
        //NSLog(@"Data_Objec=%@",[obj table_data]);
        //NSLog(@"dic=%@",dic);            
        
        //收盤價
        //double close;
        //close=[[[obj table_data] objectForKey:DB_object_quote_all_close] floatValue];
        
        //部位資訊
        
        double example;
        
        
        
        example=[[dic objectForKey:DB_onlyfortableObject_positon_EnterSample] doubleValue] *2;            
        
        
        
        //存入資料
        [dic removeObjectForKey:DB_onlyfortableObject_positon_valueChangeSample];
        [dic setObject:[NSNumber numberWithDouble:example] forKey:DB_onlyfortableObject_positon_valueChangeSample];
        
        
    }        
    //範例1
    else if ([colnum_name isEqualToString:DB_onlyfortableObject_positon_valueChangeSample1]) {
        //計算部位損益
        
        //取出股票通用物件資料
        //Data_Object *obj=[self object_data_arrary_object_from_code:code];
        //取出表格上股票資訊
        NSMutableDictionary *dic=[self object_data_arrary_only_for_table_from_code:code];
        
        //取出表格價格    
        //NSLog(@"Data_Objec=%@",[obj table_data]);
        //NSLog(@"dic=%@",dic);            
        
        //收盤價
        //double close;
        //close=[[[obj table_data] objectForKey:DB_object_quote_all_close] floatValue];
        
        //部位資訊
        
        double example;
        
        
        
        example=[[dic objectForKey:DB_onlyfortableObject_positon_EnterSample1] doubleValue] *3;            
        
        
        
        //存入資料
        [dic removeObjectForKey:DB_onlyfortableObject_positon_valueChangeSample1];
        [dic setObject:[NSNumber numberWithDouble:example] forKey:DB_onlyfortableObject_positon_valueChangeSample1];
        
        
    }  
    
    
}

//針對表格總和的特定欄位值的統計值進行計算 最後資料放的位置:data_dic_onlyfortableObject
-(void)calculate_table_sum:(NSString *)key column_item:(NSString *)column_item locationPath:(NSString *)locationpath index_before:(int)index_before{
    
    NSString *column_item_c;
    
    if (index_before==0) {
        column_item_c=column_item;
    }
    else{
        //倒數第2個以後是用:作為分割標準
        column_item_c=[NSString stringWithFormat:@"%@:%i",column_item,index_before];
    
    }
    
    //NSLog(@"針對表格總和的特定欄位值的統計值進行計算=%@",column_item);
    
    
    //標記：部位-數據是依據其他欄位資料計算出來 7.實作最後一列總和與平均計算功能 目前算法一治
    
    //標記：利用欄位屬性定義自動完成程式碼 最後一列總和與平均全部自動計算 最底層實作
   
    //加總
    if ([column_item hasSuffix:@"tablesum"]) {
       
        if (   [locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_profit]
            || [locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_temp]
            || [locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_signal]
            || [locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_indicator]) {
            
            //計算總計
            double sum_long;
            sum_long=0;
            
            for (Data_Object *db in  data_array_object ) {
                 
                //取出訊號 多空狀態
                double ls;
                
                //功能步驟：個股時間序列上新增資料集合步驟 功能步驟: 4.樹狀資料
                
                
                if ([locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_signal]) {
                                  
                    
                    ls= [[db get_last_his_data_serial_forkey:key HIS_dic_key:HIS_dic_signal index_before:index_before] doubleValue];
                    
                }
                else if ([locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_indicator]) {
                    
                    ls= [[db get_last_his_data_serial_forkey:key HIS_dic_key:HIS_dic_indicator index_before:index_before] doubleValue];
                    
                }
                else if ([locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_profit]) {
                      
                     ls= [[db get_last_his_data_serial_forkey:key HIS_dic_key:HIS_dic_profit index_before:index_before] doubleValue];
                }
                else if ([locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_temp]) {
                    
                    ls= [[db get_last_his_data_serial_forkey:key HIS_dic_key:HIS_dic_temp index_before:index_before] doubleValue];
                }
                else{
                    
                    ls=0;
                }
                
                
                sum_long=sum_long+ls;
                
                
            }
                         
            //將計算出來的存入
            {
                if ([data_dic_onlyfortableObject objectForKey:column_item_c]) {
                    
                    [data_dic_onlyfortableObject removeObjectForKey:column_item_c];
                }
                
                
                [data_dic_onlyfortableObject setObject:[NSNumber numberWithDouble: sum_long] forKey:column_item_c];
                
                
                
            }
                         
        }
        else{
            //計算總計
            double sum;
            sum=0;
            for (Data_Object *db in  data_array_object ) {
                
                NSString *code=@"";
                
                if ([[db table_data] objectForKey:DB_object_sqlite_basic_code]  ) {
                    code=[[db table_data] objectForKey:DB_object_sqlite_basic_code];
                }
                NSMutableDictionary *onlyfortableObject=[ self object_data_arrary_only_for_table_from_code:code];
                
                sum=sum+[[onlyfortableObject objectForKey:key] floatValue];
                
                
            }
            //將計算出來的存入
            {
                if ([data_dic_onlyfortableObject objectForKey:column_item_c]) {
                    
                    [data_dic_onlyfortableObject removeObjectForKey:column_item_c];
                }
                
                [data_dic_onlyfortableObject setObject:[NSNumber numberWithDouble:sum] forKey:column_item_c];
                
            }
        }
                       
    }
    //平均
    else  if ([column_item hasSuffix:@"tableaverage"]){
        if ([locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_profit] || [locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_temp]  || [locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_signal]  || [locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_indicator]) {
            
            //計算總計
            double sum_long;
            sum_long=0;
            int count=[data_array_object count];
            
            for (Data_Object *db in  data_array_object ) {
                /*
                NSString *code=@"";
                
                if ([[db table_data] objectForKey:DB_object_sqlite_basic_code]  ) {
                    code=[[db table_data] objectForKey:DB_object_sqlite_basic_code];
                }
                */
                //取出訊號 多空狀態
                double ls;
                if ([locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_signal]) {
                    
                    
                    ls= [[db get_last_his_data_serial_forkey:key HIS_dic_key:HIS_dic_signal index_before:index_before] doubleValue];
                    
                }
                else if ([locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_indicator]) {
                    
                    ls= [[db get_last_his_data_serial_forkey:key HIS_dic_key:HIS_dic_indicator index_before:index_before] doubleValue];
                }
                else if ([locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_profit]) {
                    
                    ls= [[db get_last_his_data_serial_forkey:key HIS_dic_key:HIS_dic_profit index_before:index_before] doubleValue];
                }
                else if ([locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_temp]) {
                    
                    ls= [[db get_last_his_data_serial_forkey:key HIS_dic_key:HIS_dic_temp index_before:index_before] doubleValue];
                }
                else{
                    
                    ls=0;
                }

                
                sum_long=sum_long+ls;
                 
            }
            
            
            
            //將計算出來的存入
            {
                if ([data_dic_onlyfortableObject objectForKey:column_item_c]) {
                    
                    [data_dic_onlyfortableObject removeObjectForKey:column_item_c];
                }
                
                if (count==0) {
                    [data_dic_onlyfortableObject setObject:[NSNumber numberWithDouble: 0] forKey:column_item_c];
                }
                else{
                    double sc;
                    sc=sum_long/count  ;
                    
                    [data_dic_onlyfortableObject setObject:[NSNumber numberWithDouble:  sc] forKey:column_item_c];
                    
                }
                
             
                
                
                
            }
            
            
            
        }
        else{
            //計算總計
            double sum;
            int count;
            sum=0;
            count=0;
            double average;
            
            for (Data_Object *db in  data_array_object ) {
                
                NSString *code=@"";
                if ([[db table_data] objectForKey:DB_object_sqlite_basic_code]  ) {
                    code=[[db table_data] objectForKey:DB_object_sqlite_basic_code];
                }
                NSMutableDictionary *onlyfortableObject=[ self object_data_arrary_only_for_table_from_code:code];
                
                // NSLog(@"DB_onlyfortableObject_positon_marketvalue=%@",[onlyfortableObject objectForKey:DB_onlyfortableObject_positon_marketvalue]);
                
                sum=sum+[[onlyfortableObject objectForKey:key] floatValue];
                count=count+1;
                
            }
            if (count==0) {
                average=0;
            }
            else {
                average=sum/count;
            }
            
            
            //將計算出來的存入
            {
                if ([data_dic_onlyfortableObject objectForKey:column_item_c]) {
                    
                    [data_dic_onlyfortableObject removeObjectForKey:column_item_c];
                }
                
                [data_dic_onlyfortableObject setObject:[NSNumber numberWithDouble:average] forKey:column_item_c];
                
                
                
            }
        
        }
        
                
    }
    //多單買入數量
    else  if ([column_item hasSuffix:@"tablesumLongCount"]){
        //計算總計
        int sum_long;
        sum_long=0;
        
        for (Data_Object *db in  data_array_object ) {
            /*
            NSString *code=@"";                
            
            if ([[db table_data] objectForKey:DB_object_sqlite_basic_code]  ) {
                code=[[db table_data] objectForKey:DB_object_sqlite_basic_code];  
            }                
            */
            //取出訊號 多空狀態
            int ls;
            if ([locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_signal]) {
                
                ls= [[db get_last_his_data_serial_forkey:key HIS_dic_key:HIS_dic_signal index_before:index_before] intValue];
                
            }
            else if ([locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_indicator]) {
                
                  ls= [[db get_last_his_data_serial_forkey:key HIS_dic_key:HIS_dic_indicator index_before:index_before] intValue];
            }
            else if ([locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_profit]) {
                
                  ls= [[db get_last_his_data_serial_forkey:key HIS_dic_key:HIS_dic_profit index_before:index_before] intValue];
            }
            else if ([locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_temp]) {
                
                ls= [[db get_last_his_data_serial_forkey:key HIS_dic_key:HIS_dic_temp index_before:index_before] intValue];
            }
            else{
            
                ls=0;
            }
           
            //NSLog(@"取出訊號 %@ 多空狀態=%i locationpath=%@",key, ls,locationpath );
           
            //表示狀態是多
            if ( ls > 0) {
               
                sum_long=sum_long+ls;
            }
                       
        }               
        
        
        
        //將計算出來的存入
        {
            if ([data_dic_onlyfortableObject objectForKey:column_item_c]) {
                
                [data_dic_onlyfortableObject removeObjectForKey:column_item_c];
            }
            
            
            [data_dic_onlyfortableObject setObject:[NSNumber numberWithInt: sum_long] forKey:column_item_c];
            
            
            
        }
        
    }  
    //多單比例
    else  if ([column_item hasSuffix:@"tableaverageLongCount"]){
       
        //計算總計
        double sum_long,sum;
        sum_long=0;
        sum=0;
        for (Data_Object *db in  data_array_object ) {
            /*
            NSString *code=@"";                
            
            if ([[db table_data] objectForKey:DB_object_sqlite_basic_code]  ) {
                code=[[db table_data] objectForKey:DB_object_sqlite_basic_code];  
            }                
            */
            //取出訊號 多空狀態
            int ls;
            //NSLog(@"key=%@",key);
            
            
            
            if ([locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_signal]) {
                
                //test
                //[db get_last_his_data_serial_forkey:key HIS_dic_key:HIS_dic_signal index_before:index_before] ;
                
                ls= [[db get_last_his_data_serial_forkey:key HIS_dic_key:HIS_dic_signal index_before:index_before] intValue];
                
               // NSLog(@"test= %@ %@",[db get_last_his_data_serial_forkey:key HIS_dic_key:HIS_dic_signal index_before:index_before],[db get_last_his_data_serial_forkey:key HIS_dic_key:HIS_dic_signal]);
                
            }
            else if ([locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_indicator]) {
                
                ls= [[db get_last_his_data_serial_forkey:key HIS_dic_key:HIS_dic_indicator index_before:index_before] intValue];
            }
            else if ([locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_profit]) {
                
                ls= [[db get_last_his_data_serial_forkey:key HIS_dic_key:HIS_dic_profit index_before:index_before] intValue];
            }
            else if ([locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_temp]) {
                
                ls= [[db get_last_his_data_serial_forkey:key HIS_dic_key:HIS_dic_temp index_before:index_before] intValue];
            }
            else{
                
                ls=0;
            }
            
            //限制加碼上限
            int sum_marther;//分母
            
            //如果是LFMESA 買入或是加碼數量不再是1
            if ([key isEqualToString:DB_his_data_signal_LFMESA1_State_add]) {
                
                //取出LFMESA 加碼上限
                int limit_add= [[db get_last_his_data_serial_forkey:DB_his_data_indicator_LFMESA1_Add_Limit HIS_dic_key:HIS_dic_indicator index_before:index_before] intValue];
                
                
                if (limit_add>0) {
                    sum_marther=limit_add;
                }
                else{
                    sum_marther=1;
                }
            }
            else{
                sum_marther=1;
            }
            
            //表示狀態是多
            if (ls>0) {
                sum_long=sum_long+ls;
            }
            sum=sum+sum_marther;
        }               
        
        
        
        //將計算出來的存入
        {
            if ([data_dic_onlyfortableObject objectForKey:column_item_c]) {
                
                [data_dic_onlyfortableObject removeObjectForKey:column_item_c];
            }
            if (sum>0) {
                
                //限制小數點後兩位的方式
                double f2=[[NSString stringWithFormat:@"%0.2f", sum_long/sum]doubleValue];
                
                [data_dic_onlyfortableObject setObject:[NSNumber numberWithDouble: f2] forKey:column_item_c];
            }
            else {
                [data_dic_onlyfortableObject setObject:[NSNumber numberWithDouble:0] forKey:column_item_c];
            }
            
            
        }
        
       
    }       
     
    
}

//計算全部最下面一列總和與平均的數值

//功能步驟：樹狀排序 功能步驟: 7.1.計算報價表上最下面一列數據 也就是排列準則
-(void)calcuate_table_position_sum_average_all:(NSMutableArray *)table_column_data barcount:(int)barcount data_titles:(NSArray *)data_titles{
   
    //標記：利用欄位屬性定義自動完成程式碼 最後一列總和與平均全部自動計算    
    for (NSMutableDictionary *tcd in table_column_data  ) {
       
        if ([ [tcd objectForKey:Table_Column_have_sumaverage_row] isEqualToNumber:[NSNumber numberWithBool:YES] ]) {
            
            //NSLog(@"計算全部最下面一列總和與平均的數值=%@",[tcd objectForKey:Table_Column_row_tabelsum]);
            //表示此列有要計算最下面一列
            
            //計算最下面一列的總和與平均數值
            
            for (int i=0; i<barcount; i++) {
             //從最後一個到最後100個都計算
                
                
                if ( data_titles == nil) {
                    //第一小列
                    [self calculate_table_sum:[tcd objectForKey:Table_Column_Key]
                                  column_item:[tcd objectForKey:Table_Column_row_tabelsum]
                                 locationPath:[tcd objectForKey:Table_Column_data_loactaionPath] index_before:i ];
                    
                    //第二小列
                    [self calculate_table_sum:[tcd objectForKey:Table_Column_Key]
                                  column_item:[tcd objectForKey:Table_Column_row_tableaverage]
                                 locationPath:[tcd objectForKey:Table_Column_data_loactaionPath] index_before:i];
                    
                    
                }
                else{
                    
                   
                    
                    if (!([data_titles indexOfObject:[tcd objectForKey:Table_Column_row_tabelsum]]==NSNotFound)) {
                                               
                        //第一小列
                        [self calculate_table_sum:[tcd objectForKey:Table_Column_Key]
                                      column_item:[tcd objectForKey:Table_Column_row_tabelsum]
                                     locationPath:[tcd objectForKey:Table_Column_data_loactaionPath] index_before:i ];
                        
                    }
                    // NSLog(@"sum=%@",[tcd objectForKey:Table_Column_row_tableaverage]);
                    
                    if (!([data_titles indexOfObject:[tcd objectForKey:Table_Column_row_tableaverage]]==NSNotFound)) {
                      
                        
                        //第二小列
                        [self calculate_table_sum:[tcd objectForKey:Table_Column_Key]
                                      column_item:[tcd objectForKey:Table_Column_row_tableaverage]
                                     locationPath:[tcd objectForKey:Table_Column_data_loactaionPath] index_before:i];
                    
                    }
                    
                   
                    
                
                
                }
                
                
                
            }
            
            
           
              
            
        }
       
    }
    
         
}

#pragma mark -
#pragma mark 輸出 表格上的-部位-欄位
//資訊流：彙整資料至副圖  4:到該節點取出資料
-(NSMutableArray *)output_table_column_all:(NSMutableArray *)all_table_column_data table_column_single:(NSMutableDictionary *)table_column_single output_data:(NSMutableDictionary *)output_data{
   
    //除錯區
    {
        NSLog(@"輸出 output_data=%@",    output_data  );
        //output_data 內含資料
        NSLog(@"輸出 table_column_single=%@",    table_column_single  );
        
        NSLog(@"欄位 資料存放位置=%@",     [table_column_single objectForKey:Table_Column_data_loactaionPath]  );
        NSLog(@"欄位 Table_Column_hisdata_calculate_filed=%@",     [table_column_single objectForKey:Table_Column_hisdata_calculate_filed]  );
        NSLog(@"欄位 Table_Column_hisdata_calculate_methodd=%@",     [table_column_single objectForKey:Table_Column_hisdata_calculate_method]  );
        
        NSLog(@"欄位 Table_Column_Key=%@",     [table_column_single objectForKey:Table_Column_Key]  );
    }
   
    //1.每個節點下的 個股資料一一取出
    NSMutableDictionary *all_get_data=[[[NSMutableDictionary alloc] init] autorelease];//利用字典的方式存放取得所有資料
    
    for (Data_Object *db in  data_array_object ) {
      
        //如果有代碼資料
        if ([[db table_data] objectForKey:DB_object_sqlite_basic_code]  ) {
            
            //代碼
            NSString *code=[[db table_data] objectForKey:DB_object_sqlite_basic_code];
           
            //將單一股票內的所有歷史資料取出並且轉成array方式存放
            NSMutableArray *get_data=[db get_single_column_all_his_data:table_column_single];
            
             
            if (get_data) {
                //將代碼作為key 存放取出資料集合
                
                [all_get_data setObject:get_data forKey:code];
                
                
            }
            
           
        
        }
        
    }
  
    if ([all_get_data count]>0) {
        //2.進行合成
        NSMutableArray *combine_array=[self combine_his_data_array:all_get_data table_column_single:table_column_single output_data:output_data];
        
        //3.回傳出資料
        return combine_array;
        
    }
    else{
    
        return nil;
        
    }
    
  
}

//資訊流：彙整資料至副圖  5:進行合成
//將取出的單筆資料合成單一陣列
-(NSMutableArray *)combine_his_data_array:(NSMutableDictionary *)dic_data table_column_single:(NSMutableDictionary *)table_column_single output_data:(NSMutableDictionary *)output_data {

    // 
    //1.判斷要用哪一種時間作為分割點
        
    //2.時間最長與最短的時間點找出來
    int start_date=0;
    int end_date=0;
    {
        BOOL start=NO;
        for (NSMutableArray *aaray in [dic_data allValues] ) {
            
            
            for (NSMutableDictionary *dic in aaray  ) {
                int now_date=[[dic objectForKey:TransFormDate] intValue];
                if (start==NO) {
                    start=YES;
                    
                    start_date=now_date;
                    end_date=now_date;
                }
                if (start_date>= now_date) {
                    start_date=now_date;
                }
                else if (end_date<=now_date) {
                    end_date=now_date;
                    
                }
                
            }
            
        }
        
        
        //NSLog(@"開始日期 =%@ 結束日期=%@",[NSString stringWithFormat:@"%i",start_date],[NSString stringWithFormat:@"%i",end_date] );
    }
    //3.製作新的陣列
    NSMutableArray *temp_array=[[[NSMutableArray alloc] init] autorelease];
    {
       
        //將數字日期轉成NSDate
        {
            NSDate *start_date_ns;
            NSDate *end_date_ns;
            
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
            [dateFormatter setDateFormat:@"yyyyMMdd"];
            [dateFormatter setTimeZone:   [NSTimeZone timeZoneForSecondsFromGMT:0]];

            
            start_date_ns = [dateFormatter dateFromString:[NSString stringWithFormat:@"%i",start_date] ];
            end_date_ns   = [dateFormatter dateFromString:[NSString stringWithFormat:@"%i",end_date] ];
            
            
            
        
            //NSLog(@"start_date_ns=%i %@",start_date ,start_date_ns );
            //NSLog(@"end_date_ns=%i %@",end_date,end_date_ns );
        
            NSDate *step_date= start_date_ns  ;
            NSLog(@"start_date_ns=%@",start_date_ns );
            NSLog(@"end_date_ns=%@",end_date_ns );
            
            step_date=[step_date dateByAddingTimeInterval:  -24*60*60];
            do {
                
                step_date=[step_date dateByAddingTimeInterval:  24*60*60];                
                {
                    
                    
                    NSMutableDictionary *dic_temp= [[NSMutableDictionary alloc] init] ;
                    
                    [dic_temp setObject:  [dateFormatter stringFromDate: step_date] forKey:TransFormDate];
                    
                    [temp_array addObject:dic_temp];
                    
                    [dic_temp release];
                    
                    // NSLog(@"step_date=%@ %@", step_date,[dateFormatter stringFromDate: step_date] );
                }                 
            } while ([step_date compare: end_date_ns] !=NSOrderedSame);
            
            
            
            [dateFormatter release];            
        }
    }
    //4.一一將個別資料存入新的陣列
     {
        for (NSString *key in [dic_data allKeys] ){
           //針對每個股票丟入新資訊
            
            NSString *code=key;
             
            for (NSMutableDictionary *dic_code in[dic_data objectForKey:key]) {
               
                //NSNumber *valuee=[dic_code objectForKey:[table_column_single objectForKey:Table_Column_Key] ];
                
                NSMutableDictionary *all=[dic_code objectForKey:Single_all_dic  ];
                
                //找到新的陣列 相對應的位置
                NSString *search=[dic_code objectForKey:TransFormDate];
               
                
                NSUInteger idx = [temp_array  indexOfObjectPassingTest:
                                  ^ BOOL (NSMutableDictionary* obj, NSUInteger idx, BOOL *stop)
                                  {
                                      BOOL result = ([ [obj   objectForKey:TransFormDate] isEqualToString:search ]);
                                      *stop = result;
                                      return result;
                                      
                                  }];
            
            
                if (idx!=NSNotFound) {
                   //取出暫存陣列的字典
                     NSMutableDictionary *temp_dic=[temp_array objectAtIndex:idx];
                    
                    [temp_dic setObject:all forKey:code];
                    
                }
               
            }
             
        }
     
    
    }
    
    //5.填充空白陣列
     NSMutableDictionary *last_dic= [[NSMutableDictionary alloc] init] ; 
    {
      //1.建構一個存放前一期的字典
                
        for ( NSString *key in [dic_data allKeys]) {
            
            NSMutableDictionary *temp=[[[NSMutableDictionary alloc] init] autorelease];
            [last_dic setObject:temp forKey:key];
            
        }
         
        
        //NSLog(@"存放前一期的字典=%@", last_dic );
        
        //2.開始填充空白位置
        for (NSMutableDictionary *sdic in temp_array) {
           
            //針對每一筆字典取出對應應該有的資料
            NSMutableDictionary *new_last_dic=[last_dic mutableCopy];
            
            for (NSString *keyss in [last_dic allKeys]) {
                 
                if ([sdic objectForKey:keyss]) {
                    
                    //表示該時間點有資料 更新最後一筆資料
                     [new_last_dic setObject:[sdic objectForKey:keyss]  forKey:keyss  ];
                                          
                }
                else{
                    //表示該時間沒有此資料 將最後一筆資料填入此位置
                    [sdic setObject:[last_dic objectForKey:keyss] forKey:keyss];
                
                }
                 
                
            
            }
            
            [last_dic release];
           
            last_dic=[new_last_dic mutableCopy];
            
            [new_last_dic release];
            
        }
       
        
    }
    
    //6.取出資料路徑 //取出欄位訊息
    NSString *locationpath1;
    NSString *locationpath2;
    NSString *locationpath3;
    NSString *locationpath_reference_BS; //參考的買賣訊號資料位置
    BOOL isUsedForUnrealized_field;      //該欄位是否為計算未實現損益
    NSString *Long_or_Short;             //選擇的欄位是做多或是做空
    {
        //其它訊息
        locationpath_reference_BS=[table_column_single objectForKey:Table_Column_Reference_calculate_filed];
        
        isUsedForUnrealized_field =[[table_column_single objectForKey:Table_Column_is_for_Unrealized_Profit] boolValue];
        
        Long_or_Short =[table_column_single objectForKey:Table_Column_calculate_Long_or_Short];
        
        
        //路徑
        {
            NSString *locationpath= [table_column_single objectForKey:Table_Column_data_loactaionPath] ;
            
            //路徑下1層
            
            if ([locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_signal]) {
                
                locationpath1=HIS_dic_signal;
                
                
            }
            else if ([locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_indicator]) {
                locationpath1=HIS_dic_indicator;
                
            }
            else if ([locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_profit]) {
                locationpath1=HIS_dic_profit;
                
            }
            else if ([locationpath isEqualToString:loactaionPath_tableobject_data_array_object_his_data_serial_temp]) {
                locationpath1=HIS_dic_temp;
                
                
            }
            else{
                locationpath1=nil;
            }
            
            //路徑下2層
            locationpath2=[table_column_single objectForKey:Table_Column_hisdata_calculate_method];
            
            //路徑下3層
            locationpath3=[table_column_single objectForKey:Table_Column_hisdata_calculate_filed];
        }
       
    
      
    }
    
          
    //7.依照計算方法來加總陣列
    float long_limit_ratio=1.0;
    float short_limit_ratio=1.0;
    {
        //NSLog(@"計算方法=%@"   ,[output_data objectForKey:key_output_method]);
        //NSLog(@"輸出形式方法=%@",[output_data objectForKey:key_output_data]);
        
        NSString *output_calculate_method=[output_data objectForKey:key_output_method];
       
        //NSLog(@"[last_dic allKeys]=%@", [last_dic allKeys]);
       
        NSMutableArray *last_long_code_array=[[[NSMutableArray alloc] init] autorelease];
        NSMutableArray *last_short_code_array=[[[NSMutableArray alloc] init] autorelease];
        
        //哪些股票是買入狀態
       
        int long_limit_count=[[last_dic allKeys] count]*long_limit_ratio;
        
        int short_limit_count=[[last_dic allKeys] count]*short_limit_ratio;
        
        
        //NSLog(@"做多最大檔數=%i",long_limit_count);
        //NSLog(@"做空最大檔數=%i",short_limit_count);
        
        //所有資料進行哪些股票要加入到買入狀態內
        for (NSMutableDictionary *sdic in temp_array) {
            
            //選擇的是做多的欄位
            if ([Long_or_Short isEqualToString:Defaults_Calculate_long] || [Long_or_Short isEqualToString:Defaults_Calculate_longshort]) {
                NSMutableArray *enter_long_code_array=[[NSMutableArray alloc] init]; //作多進場
                NSMutableArray *exit_long_code_array=[[NSMutableArray alloc] init]; //作多出場
                NSMutableArray *temp_last_long_code_array=[last_long_code_array mutableCopy];
                
                //針對單一時間內所有股票的買賣訊號來判斷是否要加入
                for (NSString *keyss in [last_dic allKeys]) {
                    
                    int BS=[[[[[sdic objectForKey:keyss] objectForKey:HIS_dic_signal] objectForKey:locationpath2] objectForKey:locationpath_reference_BS] intValue];
                    if (BS==1) {
                        if ([temp_last_long_code_array indexOfObject:keyss]==NSNotFound) {
                            //檢查檔數是否超過多單比例限制
                            if ([temp_last_long_code_array count]<long_limit_count) {
                                [temp_last_long_code_array addObject:keyss];
                                [enter_long_code_array addObject:keyss];
                            }
                            
                        }
                        
                    }
                    else if (BS==-1){
                        if (!([temp_last_long_code_array indexOfObject:keyss]==NSNotFound)) {
                            [temp_last_long_code_array removeObject:keyss];
                            [exit_long_code_array addObject:keyss];
                        }
                    }
                    
                    
                }
                
                [sdic setObject:temp_last_long_code_array forKey:Long_code_array];
                [sdic setObject:exit_long_code_array forKey:Exit_Long_code_array];
                [sdic setObject:enter_long_code_array forKey:Enter_Long_code_array];
                
               // [last_long_code_array release];
                
                last_long_code_array=[temp_last_long_code_array mutableCopy];
                
                [temp_last_long_code_array release];
                
                
            }
            
            //選擇的是做空的欄位
            if ([Long_or_Short isEqualToString:Defaults_Calculate_short] || [Long_or_Short isEqualToString:Defaults_Calculate_longshort]) {
                NSMutableArray *enter_short_code_array=[[NSMutableArray alloc] init];//做空進場
                NSMutableArray *exit_short_code_array=[[NSMutableArray alloc] init];//做空出場
                NSMutableArray *temp_last_short_code_array=[last_short_code_array mutableCopy];
                
                //針對單一時間內所有股票的買賣訊號來判斷是否要加入
                for (NSString *keyss in [last_dic allKeys]) {
                    
                    int BS=[[[[[sdic objectForKey:keyss] objectForKey:HIS_dic_signal] objectForKey:locationpath2] objectForKey:locationpath_reference_BS] intValue];
                    
                    if (BS==-1) {
                        if ([temp_last_short_code_array indexOfObject:keyss]==NSNotFound) {
                            //檢查檔數是否超過多單比例限制
                            if ([temp_last_short_code_array count]<short_limit_count) {
                                [temp_last_short_code_array addObject:keyss];
                                [enter_short_code_array addObject:keyss];                                
                            }
                            
                        }
                        
                    }
                    else if (BS==1){
                        if (!([temp_last_short_code_array indexOfObject:keyss]==NSNotFound)) {
                            [temp_last_short_code_array removeObject:keyss];
                            [exit_short_code_array addObject:keyss];
                        }
                    }
                    
                    
                }
                
                [sdic setObject:temp_last_short_code_array forKey:Short_code_array];
                [sdic setObject:exit_short_code_array forKey:Exit_Short_code_array];
                [sdic setObject:enter_short_code_array forKey:Enter_Short_code_array];
                
                //[last_short_code_array release];
                
                last_short_code_array=[temp_last_short_code_array mutableCopy];
                
                [temp_last_short_code_array release];
                
                
            }
            
             
        }
       
         
        //可以顯示時間序列有哪些股票是買入狀態
        /*
        for (NSMutableDictionary *sdic in temp_array){
      
             NSLog(@"<%@ = long=%@ short=%@ exit_long=%@ exit_short=%@ enter_long=%@ enter_short=%@> ",[sdic objectForKey:TransFormDate],[sdic objectForKey:Long_code_array],[sdic objectForKey:Short_code_array],[sdic objectForKey:Exit_Long_code_array],[sdic objectForKey:Exit_Short_code_array],[sdic objectForKey:Enter_Long_code_array],[sdic objectForKey:Enter_Short_code_array]);
        } 
         */
        
        
        
        
        //加總
        if ([output_calculate_method isEqualToString:_sort_by_table_object_count]) {
            
            //尚未出場的未實現損益
            if (isUsedForUnrealized_field==YES) {
                                    
                for (NSMutableDictionary *sdic in temp_array) {
                    double sum;
                    sum=0;
                    //因為多空不同所有取出不同的存放多空股票的陣列
                    NSMutableArray *code_Array;
                   
                    if ([Long_or_Short isEqualToString:Defaults_Calculate_long] || [Long_or_Short isEqualToString:Defaults_Calculate_longshort]) {
                       
                        code_Array=[sdic objectForKey:Long_code_array];
                        
                        
                        for (NSString *keyss in code_Array) {
                            
                            //階層二：每筆時間下的每個股票資料 一一家總
                            
                            if ([[[[sdic objectForKey:keyss] objectForKey:locationpath1] objectForKey:locationpath2] objectForKey:locationpath3]){
                                
                                sum=sum+[[[[[sdic objectForKey:keyss] objectForKey:locationpath1] objectForKey:locationpath2] objectForKey:locationpath3] doubleValue];
                            }
                            else{
                                sum=sum+0;
                            }
                        }
                    }
                    
                    if([Long_or_Short isEqualToString:Defaults_Calculate_short] || [Long_or_Short isEqualToString:Defaults_Calculate_longshort]){
                       
                        code_Array=[sdic objectForKey:Short_code_array];
                        
                        
                        for (NSString *keyss in code_Array) {
                            
                            //階層二：每筆時間下的每個股票資料 一一家總
                            
                            if ([[[[sdic objectForKey:keyss] objectForKey:locationpath1] objectForKey:locationpath2] objectForKey:locationpath3]){
                                
                                sum=sum+[[[[[sdic objectForKey:keyss] objectForKey:locationpath1] objectForKey:locationpath2] objectForKey:locationpath3] doubleValue];
                            }
                            else{
                                sum=sum+0;
                            }
                        }
                        
                    } 
                    
                    //將計算結果添入
                    [sdic setObject:[NSNumber numberWithDouble:sum] forKey:output_calculate_method];
            
                }                 
            }
            
            //出場之後損益才入帳方法=>累積已經實現損益
            else{
                
                NSMutableDictionary *sdic; //本期字典
                NSMutableDictionary *sdic_next; //下一期字典 因為訊號出現後 是以下一期的開盤價出場
                double sum; //累計的已經實現損益總和 各期加上所有股票總和
                sum=0;
                
                for (int i=0; i<[temp_array count]; i++) {
                    
                    //本期字典
                    sdic=[temp_array objectAtIndex:i];
                    //下一期字典
                    if (i+1<[temp_array count]) {
                        //下一期
                        sdic_next=[temp_array objectAtIndex:i+1];
                    }
                    else{
                        //超過最後一期之後，就用本期
                         sdic_next=[temp_array objectAtIndex:i];                    
                    }
                    
                    //因為多空不同所有取出不同的存放多空股票的陣列
                    NSMutableArray *code_Array;
                    if ([Long_or_Short isEqualToString:Defaults_Calculate_long] || [Long_or_Short isEqualToString:Defaults_Calculate_longshort]) {
                        
                        code_Array=[sdic objectForKey:Exit_Long_code_array];
                        
                        //進入循環加總
                        for (NSString *keyss in code_Array){
                            //累計損計：是以本期與下一期的累計已經損益差值當作差值
                            sum=sum+[[[[[sdic_next objectForKey:keyss] objectForKey:locationpath1] objectForKey:locationpath2] objectForKey:locationpath3] doubleValue]-[[[[[sdic objectForKey:keyss] objectForKey:locationpath1] objectForKey:locationpath2] objectForKey:locationpath3] doubleValue] ;
                        }
                        
                    }
                    
                    if([Long_or_Short isEqualToString:Defaults_Calculate_short] || [Long_or_Short isEqualToString:Defaults_Calculate_longshort]){
                        
                        code_Array=[sdic objectForKey:Exit_Short_code_array];
                        
                        //進入循環加總
                        for (NSString *keyss in code_Array){
                            //累計損計：是以本期與下一期的累計已經損益差值當作差值
                            sum=sum+[[[[[sdic_next objectForKey:keyss] objectForKey:locationpath1] objectForKey:locationpath2] objectForKey:locationpath3] doubleValue]-[[[[[sdic objectForKey:keyss] objectForKey:locationpath1] objectForKey:locationpath2] objectForKey:locationpath3] doubleValue] ;
                        }
                    } 
                    
                    
                    //將計算結果添入
                    [sdic setObject:[NSNumber numberWithDouble:sum] forKey:output_calculate_method];
                    
                }
            } 
            
        }
        //平均
        else if ([output_calculate_method isEqualToString:_sort_by_table_object_average]) {
            //尚未出場的未實現損益
            if (isUsedForUnrealized_field==YES) {
                
                for (NSMutableDictionary *sdic in temp_array) {
                    
                    double sum;
                    sum=0;
                    //因為多空不同所有取出不同的存放多空股票的陣列
                    NSMutableArray *code_Array;
                    
                    if ([Long_or_Short isEqualToString:Defaults_Calculate_long] || [Long_or_Short isEqualToString:Defaults_Calculate_longshort]) {
                        
                        code_Array=[sdic objectForKey:Long_code_array];
                        
                        
                        for (NSString *keyss in code_Array) {
                            
                            //階層二：每筆時間下的每個股票資料 一一家總
                            
                            if ([[[[sdic objectForKey:keyss] objectForKey:locationpath1] objectForKey:locationpath2] objectForKey:locationpath3]){
                                
                                sum=sum+[[[[[sdic objectForKey:keyss] objectForKey:locationpath1] objectForKey:locationpath2] objectForKey:locationpath3] doubleValue];
                            }
                            else{
                                sum=sum+0;
                            }
                        }
                    }
                    
                    if([Long_or_Short isEqualToString:Defaults_Calculate_short] || [Long_or_Short isEqualToString:Defaults_Calculate_longshort]){
                        
                        code_Array=[sdic objectForKey:Short_code_array];
                        
                        
                        for (NSString *keyss in code_Array) {
                            
                            //階層二：每筆時間下的每個股票資料 一一家總
                            
                            if ([[[[sdic objectForKey:keyss] objectForKey:locationpath1] objectForKey:locationpath2] objectForKey:locationpath3]){
                                
                                sum=sum+[[[[[sdic objectForKey:keyss] objectForKey:locationpath1] objectForKey:locationpath2] objectForKey:locationpath3] doubleValue];
                            }
                            else{
                                sum=sum+0;
                            }
                        }
                        
                    }
                    
                    //平均換算
                    int total_count=[[last_dic allKeys] count];
                    float average;
                    if (total_count>0) {
                        
                        if ([Long_or_Short isEqualToString:Defaults_Calculate_long]){
                        
                            average=(sum/total_count)/ long_limit_ratio ; //依照限制比例在調整回去
                        }
                        else if ([Long_or_Short isEqualToString:Defaults_Calculate_short]){
                            average=(sum/total_count)/ short_limit_ratio;
                        }
                        else if ([Long_or_Short isEqualToString:Defaults_Calculate_longshort]){
                            float avearge_limit_ratio=(short_limit_ratio+long_limit_ratio)/2;
                            average=(sum/total_count)/ avearge_limit_ratio;
                        }
                        
                        else{
                           average=0;
                        }
                       
                    
                    }
                    else{
                        average=0;
                    }
                    //將計算結果添入
                    [sdic setObject:[NSNumber numberWithDouble:average] forKey:output_calculate_method];
                    
                }
            }
            
            //出場之後損益才入帳方法=>累積已經實現損益
            else{
                
                NSMutableDictionary *sdic; //本期字典
                NSMutableDictionary *sdic_next; //下一期字典 因為訊號出現後 是以下一期的開盤價出場
                double sum; //累計的已經實現損益總和 各期加上所有股票總和
                sum=0;
                
                for (int i=0; i<[temp_array count]; i++) {
                    
                    //本期字典
                    sdic=[temp_array objectAtIndex:i];
                    //下一期字典
                    if (i+1<[temp_array count]) {
                        //下一期
                        sdic_next=[temp_array objectAtIndex:i+1];
                    }
                    else{
                        //超過最後一期之後，就用本期
                        sdic_next=[temp_array objectAtIndex:i];
                    }
                    
                    //因為多空不同所有取出不同的存放多空股票的陣列
                    //因為多空不同所有取出不同的存放多空股票的陣列
                    NSMutableArray *code_Array;
                    if ([Long_or_Short isEqualToString:Defaults_Calculate_long] || [Long_or_Short isEqualToString:Defaults_Calculate_longshort]) {
                        
                        code_Array=[sdic objectForKey:Exit_Long_code_array];
                        
                        //進入循環加總
                        for (NSString *keyss in code_Array){
                            //累計損計：是以本期與下一期的累計已經損益差值當作差值
                            sum=sum+[[[[[sdic_next objectForKey:keyss] objectForKey:locationpath1] objectForKey:locationpath2] objectForKey:locationpath3] doubleValue]-[[[[[sdic objectForKey:keyss] objectForKey:locationpath1] objectForKey:locationpath2] objectForKey:locationpath3] doubleValue] ;
                        }
                        
                    }
                    
                    if([Long_or_Short isEqualToString:Defaults_Calculate_short] || [Long_or_Short isEqualToString:Defaults_Calculate_longshort]){
                        
                        code_Array=[sdic objectForKey:Exit_Short_code_array];
                        
                        //進入循環加總
                        for (NSString *keyss in code_Array){
                            //累計損計：是以本期與下一期的累計已經損益差值當作差值
                            sum=sum+[[[[[sdic_next objectForKey:keyss] objectForKey:locationpath1] objectForKey:locationpath2] objectForKey:locationpath3] doubleValue]-[[[[[sdic objectForKey:keyss] objectForKey:locationpath1] objectForKey:locationpath2] objectForKey:locationpath3] doubleValue] ;
                        }
                    }
                    
                    //平均換算
                    int total_count=[[last_dic allKeys] count];
                    float average;
                   
                    if (total_count>0) {
                        
                        if ([Long_or_Short isEqualToString:Defaults_Calculate_long]){
                            
                            average=(sum/total_count)/ long_limit_ratio ; //依照限制比例在調整回去
                        }
                        else if ([Long_or_Short isEqualToString:Defaults_Calculate_short]){
                            average=(sum/total_count)/ short_limit_ratio;
                        }
                        else if ([Long_or_Short isEqualToString:Defaults_Calculate_longshort]){
                            
                            float avearge_limit_ratio=(short_limit_ratio+long_limit_ratio)/2;
                            
                            average=(sum/total_count)/ avearge_limit_ratio;
                        }
                        
                        
                        else{
                            average=0;
                        }
                        
                        
                    }
                    else{
                        average=0;
                    }
                    
                    
                    //將計算結果添入
                    [sdic setObject:[NSNumber numberWithDouble:average] forKey:output_calculate_method];
                    
                }
            }
            
        }
        //多單數目
        else if ([output_calculate_method isEqualToString:_sort_by_long_count]) {
            //階層一：每筆時間一一計算
            for (NSMutableDictionary *sdic in temp_array) {
                 
                int long_count;
                long_count=0;
                
                for (NSString *keyss in [last_dic allKeys]) {
                    //階層二：每筆時間下的每個股票資料 一一家總
                    if ([[[[sdic objectForKey:keyss] objectForKey:locationpath1] objectForKey:locationpath2] objectForKey:locationpath3]){
                        
                        //做多狀態判斷
                        if ([[[[[sdic objectForKey:keyss] objectForKey:locationpath1] objectForKey:locationpath2] objectForKey:locationpath3] intValue]>0){
                            
                            long_count=long_count+[[[[[sdic objectForKey:keyss] objectForKey:locationpath1] objectForKey:locationpath2] objectForKey:locationpath3] intValue];
                        }
                        
                    }
                    
                }
                
                //將計算結果添入
                                
                [sdic setObject:[NSNumber numberWithDouble:long_count] forKey:output_calculate_method];
            }
            
        }
        //多單比例
        else if ([output_calculate_method isEqualToString:_sort_by_long_ratio]) {
            //階層一：每筆時間一一計算
            for (NSMutableDictionary *sdic in temp_array) {
                
                double long_count;
                long_count=0;
                double total_count;
                total_count=0;
                
                for (NSString *keyss in [last_dic allKeys]) {
                    
                    //階層二：每筆時間下的每個股票資料 一一家總
                    if ([[[[sdic objectForKey:keyss] objectForKey:locationpath1] objectForKey:locationpath2] objectForKey:locationpath3]){
                    
                        //做多狀態判斷
                        if ([[[[[sdic objectForKey:keyss] objectForKey:locationpath1] objectForKey:locationpath2] objectForKey:locationpath3] intValue]>0){
                           
                            
                            int child=[[[[[sdic objectForKey:keyss] objectForKey:locationpath1] objectForKey:locationpath2] objectForKey:locationpath3] intValue];
                            
                            //NSLog(@"child key=%@ 1=%@ 2=%@  3=%@ %i",keyss,locationpath1,locationpath2,locationpath3,child);
                            
                            
                            
                            long_count=long_count+child;
                      
                        }
                        
                    }
                    
                    //CL_his_data_signal_LFMESA1_State_aa1
                    //如果是LFMESA 買入或是加碼數量不再是1
                    int parent;
                    if ([locationpath3 isEqualToString:CL_his_data_signal_LFMESA1_State_add]) {
                        
                   
                        if (parent>0) {
                             parent= [[[[[sdic objectForKey:keyss] objectForKey:HIS_dic_indicator] objectForKey:locationpath2] objectForKey:CL_his_data_indicator_LFMESA1_Add_Limit] intValue];
                        }
                        else{
                            parent=1;
                        
                        }
                       
                                                
                    
                    }
                    else{
                        parent=1;
                    }
                    
                    total_count=total_count+parent;
                    
                    
                }
                
                //將計算結果添入
                double ratio;
                if (total_count>0) {
                    ratio=long_count/total_count;
                }
                else{
                    ratio=0;
                }
                
                [sdic setObject:[NSNumber numberWithDouble:ratio] forKey:output_calculate_method];
            }
            
        }
    }
    
    
    [last_dic release];
    return temp_array;
    
}



@end
